'''
Author: didiplus
Date: 2024-09-30 15:35:21
LastEditTime: 2024-09-30 15:37:39
LastEditors: didiplus
Description: 
FilePath: \pythonscript\python\趣学Python算法100例\14、个人所得税\demo.py
'''
TAXBASE = 2000

# 分为9个阶段，每个阶段包含起始点、截止点和税率
TaxTable = [
    (0, 500, 0.05),
    (500, 2000, 0.10),
    (2000, 5000, 0.15),
    (5000, 20000, 0.20),
    (20000, 40000, 0.25),
    (40000, 60000, 0.30),
    (60000, 80000, 0.35),
    (80000, 100000, 0.40),
    (100000, float('inf'), 0.45)  # 用float('inf')表示无穷大
]

# 计算税收
def calculate_tax(profit):
    tax = 0.0
    profit -= TAXBASE  # 超过个税起征点的收入

    if profit <= 0:
        return tax  # 如果没有应纳税收入，直接返回

    for start, end, rate in TaxTable:
        if profit > start:
            taxable_income = min(profit, end) - start
            tax += taxable_income * rate
            profit -= taxable_income

            print(f"征税范围：{start:6}～{end:6}  该范围内缴税金额：{taxable_income * rate:6.2f}  超出该范围的金额：{profit:.2f}")

            if profit <= 0:
                break  # 如果利润不足以再征税，退出循环

    return tax

if __name__ == '__main__':
    profit = float(input("请输入个人收入金额: "))
    tax = calculate_tax(profit)
    print(f"您的个人所得税为 {tax:.2f}")
